import { defineStore } from "pinia";
import Cookies from "js-cookie";
import { getQueryObject } from "../utils/index";

let viewWidthStr = localStorage.getItem("HC_ViewWidth");
let appViewWidth = {};
if (viewWidthStr) {
  appViewWidth = JSON.parse(viewWidthStr);
}

export const useAppStore = defineStore("app", {
  state: () => ({
    sidebar: {
      opened: localStorage.getItem("sidebarStatus")
        ? !!+localStorage.getItem("sidebarStatus")
        : true,
      withoutAnimation: false,
      hide: false,
    },
    appFsCache: {},
    // 视图列表缓存
    appViewCache: {},
    // 视图宽度记录
    appViewWidth: appViewWidth,
    device: "desktop",
    VisitedUnidList: [],
    noShowLayout: false,
    topMenuItem: {}, // 当前选中的模块分类
    currentRoutePath: "", // 当前路由路径（解决点击左侧菜单，右侧刷新两次）
    size: localStorage.getItem("size") || "biaozhun",
  }),
  actions: {
    // 添加访问过的UNID
    addVisitedUnidList(unid) {
      this.VisitedUnidList.push(unid);
    },
    // 切换侧边栏
    toggleSidebar() {
      if (this.sidebar.hide) {
        return false;
      }
      this.sidebar.opened = !this.sidebar.opened;
      this.sidebar.withoutAnimation = false;
      if (this.sidebar.opened) {
        localStorage.setItem("sidebarStatus", 1);
      } else {
        localStorage.setItem("sidebarStatus", 0);
      }
    },
    // 清除视图缓存
    clearViewCache() {
      this.appViewCache = {};
    },
    // 清除其他视图缓存，只保留当前视图
    clearViewCacheOther(view) {
      if (view.meta && view.meta.urlparams) {
        let urlParams = getQueryObject(view.meta.urlparams);
        if (urlParams) {
          let cacheKey =
            view.path + "_" + urlParams.modulename + "_" + urlParams.viewtype;
          let tampObj = this.appViewCache[cacheKey];
          if (tampObj) {
            this.appViewCache = {};
            this.appViewCache[cacheKey] = tampObj;
          } else {
            this.appViewCache = {};
          }
        } else {
          this.appViewCache = {};
        }
      } else {
        this.appViewCache = {};
      }
    },
    // 保存视图缓存
    saveViewCache(viewList) {
      for (let k in this.appViewCache) {
        let findIndex = viewList.findIndex((pre_view) => {
          return k.indexOf(pre_view.path) > -1;
        });
        if (findIndex == -1) {
          this.appViewCache[k] = null;
        }
      }
    },
    // 按key清除视图缓存
    clearViewCacheByKey(view) {
      if (view.meta && view.meta.urlparams) {
        let urlParams = getQueryObject(view.meta.urlparams);
        if (urlParams) {
          let cacheKey =
            view.path + "_" + urlParams.modulename + "_" + urlParams.viewtype;
          if (this.appViewCache[cacheKey]) {
            this.appViewCache[cacheKey] = null;
          }
        }
      }
    },
    // 关闭侧边栏
    closeSidebar(withoutAnimation) {
      localStorage.setItem("sidebarStatus", 0);
      this.sidebar.opened = false;
      this.sidebar.withoutAnimation = withoutAnimation;
    },
    // 切换设备类型
    toggleDevice(device) {
      this.device = device;
    },
    // 设置尺寸
    setSize(size) {
      this.size = size;
      localStorage.setItem("size", size);
    },
    // 设置当前路由路径
    setCurrentRoutePath(path) {
      this.currentRoutePath = path;
    },
    // 设置侧边栏隐藏状态
    setSidebarHide(status) {
      this.sidebar.hide = status;
    },
    // 设置是否显示布局
    setNoShowLayout(value) {
      this.noShowLayout = value;
    },
    // 设置顶部菜单项
    setTopMenuItem(value) {
      this.topMenuItem = value;
    },
    // 保存视图宽度
    saveViewWidth(P_WidthInfo) {
      /**
       * P_WidthInfo : {
       *  key: 页面标识,
       *  columnName:列表名,
       *  width: 列宽度
       * }
       */
      if (!this.appViewWidth[P_WidthInfo.key]) {
        this.appViewWidth[P_WidthInfo.key] = {};
      }
      this.appViewWidth[P_WidthInfo.key][P_WidthInfo.columnName] =
        P_WidthInfo.width;
      // 持久化
      let viewWidthStr = JSON.stringify(this.appViewWidth);
      localStorage.setItem("HC_ViewWidth", viewWidthStr);
    },
  },
});
